package cn.sccl.dirms.monitor.controller;

import cn.sccl.common.zabbix.Zabbix;

import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by pcrmsapp on 2017/4/6.
 */
public class CommonController {

    /**
     * 判断主机是否存在
     * @param ipStr
     * @return
     */
    public  boolean hasExistHost(String ipStr){
        Zabbix zabbix = Zabbix.getZabbix();
        boolean value =false;
        Map<String,Object> params = new HashMap<String,Object>();
        params.put("output","extend");
        Map<String,String> maphosts = new HashMap<>();
        maphosts.put("host",ipStr);
        params.put("filter",maphosts);

        Map<String, Object> result = zabbix.call("host.get", params);
        List<Map<String, Object>> mapList = (List<Map<String, Object>>) result.get("result");
        if(mapList.size() >0){
            value =true;
        }
        return  value;
    }

    /**
     * 获取所有的host，用于判断host是否存在
     */
    public Map<String, Object> listHost() {
        Zabbix zabbix = Zabbix.getZabbix();
        Map<String,Object> params = new HashMap<String,Object>();
        params.put("output","extend");
        Map<String, Object> result = zabbix.call("host.get", params);
        List<Map<String, Object>> mapList = (List<Map<String, Object>>) result.get("result");
        Map<String, Object> mapReslt = new HashMap<>();
        for (Map<String, Object> item: mapList) {
            String ipaddr = (String) item.get("host");
            mapReslt.put(ipaddr, item);
        }
        return mapReslt;
    }

    /**
     * 判断主机是否存在
     * @param ipStr IP地址
     * @param allHosts 所有的host
     * @Return 返回hostid
     */
    public String getHostId(String ipStr, Map<String, Object> allHosts) {
        String hostid = null;
        if(allHosts.containsKey(ipStr)) {
            Map<String, String> hostItem = (Map<String, String>) allHosts.get(ipStr);
            hostid = hostItem.get("hostid");
        }
        return hostid;
    }



    /**
     * 查询主机id
     * @param ipStr
     * @return
     */
    public   String  queryforHostByIP(String  ipStr){
        Zabbix zabbix = Zabbix.getZabbix();
        Map<String,Object> params = new HashMap<String,Object>();
        params.put("output","extend");
        Map<String,String> maphosts = new HashMap<>();
        maphosts.put("host",ipStr);
        params.put("filter",maphosts);
        String value ="";
        Map<String, Object> result = zabbix.call("host.get", params);
        List<Map<String, Object>> mapList = (List<Map<String, Object>>) result.get("result");
        if(mapList.size() >0){
           for(Map map : mapList){
               if((null != map) && (null !=map.get("hostid"))){
                   value = map.get("hostid").toString();
               }
           }
        }
        return  value;
    }


    /**
     * 将元素转换为list
     * @param hubelementgroup
     * @return
     */
    public  List<Map<String,String>> createElementsAndLinksList(
            String  hostid ,
            Map<String,Map<String,String>>   hubelementgroup, //元素分组
            Map<String,Map<String,String>>   vlanelementgroup,
            String mapid
                                                       ){
        List<Map<String,String>> firstmap  = new ArrayList<>();
        List<Map<String,String>> secondmap = new ArrayList<>();
        List<Map<String,String>> thridmap  = new ArrayList<>();
        List<Map<String,String>> allmap = new ArrayList<>();
        List<Map<String,String>> result = new ArrayList<>();
        Map<String,String>  keymaps = new HashMap<>();
        //得到第一层的元素id
        //keymaps.g
        Map<String,String> firstelements  =  genElementMap(hostid);
        firstmap.add(firstelements);
        keymaps.put(hostid,hostid);

        //得到第二层元素
        for(String  str  : hubelementgroup.keySet()) {
            Map map = hubelementgroup.get(str);
            if (null != map) {
                for (Object  obj : map.keySet()) {
                    String  key = obj.toString();//elmentid
                    String strhostid = (String)map.get(key);
                    if(!strhostid.isEmpty() && (!strhostid.equals("")) && (!str.equals(""))   && (null != key) && (!key.equals(""))) {
                        if(!key.equals(hostid)) {
                            secondmap.add(genElementMap(strhostid, str, key));
                            keymaps.put(key,key);
                        }
                    }
                }
            }
        }

        //得到第三层元素

        for(String str  : vlanelementgroup.keySet()) {
            Map map = vlanelementgroup.get(str);
            if (null != map) {
                for (Object obj : map.keySet()) {
                    String  key = obj.toString();
                    String strhostid = (String)map.get(key);
                    if(!strhostid.isEmpty() && (!strhostid.equals("")) && (!str.equals("")) && (null != key) && (!key.equals(""))) {
                        if(!hasCreateElemment(key,keymaps)) {
                            thridmap.add(genElementMap(strhostid, str, key));
                            keymaps.put(key, key);
                        }
                    }
                }
            }
        }

        allmap.addAll(firstmap);
        allmap.addAll(secondmap);
        allmap.addAll(thridmap);
        //创建第一层和第二层之间的连线关系
       // createElement
         createElementAndLinks(allmap,mapid,hostid,hubelementgroup,vlanelementgroup);
        return  result;
    }

    /**
     *  判断当前拓扑图是否已经创建元素
     */

    public boolean hasCreateElemment(String key,Map<String,String> allmap){
        boolean result = false;
        if(allmap.size() >0){
            String str = allmap.get(key);
            if((null != str) && (!str.equals(""))){
                 result = true;
            }
        }
        return  result;
    }


    /**
     * 将元素转换为list
     * @param vlanelementgroup
     * @return
     */
    public  List<Map<String,String>> createElementsAndLinksList(
            String  hostid ,
            Map<String,Map<String,String>>   vlanelementgroup){
        List<Map<String,String>> result = new ArrayList<>();
        for(Map map  : vlanelementgroup.values()) {
            if (null != map) {
                for (Object obj : map.values()) {
                    String strhostid = obj.toString();
                    result.add(genElementMap(strhostid));
                }
            }
        }
        return  result;
    }

    /**
     * 产生元素的map  ，根据情况来增加元素属性
     * @param hostid   ,
     * @return
     */
    public  Map<String,String>  genElementMap(String hostid){
        Map<String,String> result = new HashMap<>();
        result.put("elementid",hostid);
        result.put("elementtype","0");
        result.put("iconid_off","2");
        result.put("x","1");
        result.put("y","1");
        result.put("label",hostid);
        return  result;
    }

    /**
     * 产生元素的map  ，根据情况来增加元素属性
     * @param hostid   ,
     * @return
     */
    public  Map<String,String>  genElementMap(String hostid, String vlan,String elementid ){
        Map<String,String> result = new HashMap<>();
        result.put("elementid",elementid);
        result.put("elementtype","0");
        result.put("iconid_off","2");
        result.put("x","1");
        result.put("y","1");
        result.put("label",vlan+"-"+hostid.replace(".","-"));
        return  result;
    }


    /**
     * 创建元素
     * @param elementlist
     * @param mapid
     * @return
     */
    public  void createElementAndLinks(List<Map<String,String>> elementlist ,
                                              String mapid,
                                              String hostid,
                                              Map<String,Map<String,String>>   hubelementgroup,
                                              Map<String,Map<String,String>>   vlanelementgroup){
        Zabbix zabbix = Zabbix.getZabbix();
        Map<String,Object> params = new HashMap<String,Object>();
        params.put("output","extend");
        params.put("sysmapid",mapid);
        List<Map<String,String>> tmplinkmapslisk = new ArrayList<>();
        params.put("selectSelements","extend");
        params.put("selements",elementlist);
        params.put("links",tmplinkmapslisk);
        Map<String, Object> result = zabbix.call("map.update", params);
        Map<String, Object> mapList = (Map<String, Object>) result.get("result");
        Map<String,String>  value = new HashMap<>();
        List<Map<String,String>> linkmapslisk = new ArrayList<>();
        //重新get一次
        Map<String,Object> params2 = new HashMap<String,Object>();
        params2.put("output","extend");
        params2.put("sysmapids",mapid);
        params2.put("selectSelements","extend");
      //  params2.put("selectLinks","extend");
        params2.put("selectUsers","extend");
        params2.put("selectUserGroups","extend");
      //  Map<String,String> filter = new HashMap<>();
       // filter.put("sysmapid",mapid);
     //   params2.put("filter",filter);
        Map<String, Object> result2   = zabbix.call("map.get", params2);
        List<Map<String,Object>> mapList2 = (List<Map<String,Object>>)result2.get("result");

      //  List<Map<String, Object>> mapList2 = (List<Map<String, Object>>) result.get("result");
        if(mapList2.size() >0){
            if(null !=mapList2.get(0).get("selements")){
                List<Map<String, Object>>  resultlist= (List<Map<String, Object>>)mapList2.get(0).get("selements");
                if(resultlist.size() >0){
                    String firsthostselecteleid = "";
                    Map<String,Map<String,String>>  secondmaps = new HashMap<>();
                    Map<String,Map<String,String>>  thirdmaps = new HashMap<>();
                    //取得最中心交换机的id
                    for(Map<String, Object>  obj : resultlist){
                        if(null != obj){
                            String  elementid  = obj.get("elementid").toString();
                            String  selementid = obj.get("selementid").toString();
                            String  label = obj.get("label").toString();

                            if(elementid.equals(hostid)){  //是第一层
                                firsthostselecteleid =  selementid;
                                if(isExisthost(elementid,hubelementgroup)){
                                    if(label.contains("-")) {
                                        String vlan =getvlan(elementid, hubelementgroup);
                                        Map<String, String> tmpsecond = new HashMap<>();
                                        tmpsecond = secondmaps.get(vlan);
                                        if (null == tmpsecond) {
                                            tmpsecond = new HashMap<>();
                                        }
                                        tmpsecond.put(selementid, elementid);
                                        secondmaps.put(vlan, tmpsecond);
                                    }else{
                                        String vlan = getvlan(elementid,hubelementgroup);
                                        Map<String, String> tmpsecond = new HashMap<>();
                                        tmpsecond = secondmaps.get(vlan);
                                        if (null == tmpsecond) {
                                            tmpsecond = new HashMap<>();
                                        }
                                        tmpsecond.put(selementid, elementid);
                                        secondmaps.put(vlan, tmpsecond);
                                    }
                                }
                            }else if(isExisthost(elementid,hubelementgroup)){ //是第二层
                                if(label.contains("-")) {
                                    String vlan = label.substring(0, label.indexOf("-"));
                                    Map<String, String> tmpsecond = new HashMap<>();
                                    tmpsecond = secondmaps.get(vlan);
                                    if (null == tmpsecond) {
                                        tmpsecond = new HashMap<>();
                                    }
                                    tmpsecond.put(selementid, elementid);
                                    secondmaps.put(vlan, tmpsecond);
                                }
                            }else{ //剩余的第三层
                                if(label.contains("-")) {
                                    String vlan = label.substring(0,label.indexOf("-"));
                                    Map<String,String>  tmpthird = thirdmaps.get(vlan);
                                    if(null == tmpthird){
                                        tmpthird = new HashMap<>();
                                    }
                                    tmpthird.put(selementid,elementid);
                                    thirdmaps.put(vlan,tmpthird);
                                }
                            }
                          //  hubelementgroup.
                        }
                    }
                    //
                    for(String  vlanstr :  secondmaps.keySet()){
                        Map<String,String>  map2 = secondmaps.get(vlanstr);

                        for(String str2  :map2.keySet())
                            if (null != str2) {
                                //第一级与第二级的关系
                                if(!firsthostselecteleid.equals(str2)) {
                                    linkmapslisk.add(genelementLink(firsthostselecteleid, str2));
                                }
                                //第二级与第三级关系
                                Map<String,String> map3 = thirdmaps.get(vlanstr);
                                if(null != map3 ) {
                                    for (String str3 : map3.keySet()) {
                                        if (null != str3) {
                                            if(!str2.equals(str3)) {
                                                linkmapslisk.add(genelementLink(str2, str3));
                                            }
                                        }
                                    }
                                }
                            }
                    }
                    //重新提交
                    Map<String,Object> paramslink = new HashMap<String,Object>();
                    paramslink.put("output","extend");
                    paramslink.put("sysmapid",mapid);
                    paramslink.put("selements",resultlist);
                    paramslink.put("links",linkmapslisk);
                    Map<String, Object> result3 = zabbix.call("map.update", paramslink);
                    Map<String, Object> mapList3 = (Map<String, Object>) result3.get("result");
                }
            }
        }
    }

    public  boolean isExisthost(String elementid ,Map<String,Map<String,String>>  hubelementgroup){
        boolean result = false;
        for(Map<String,String> map : hubelementgroup.values()){
             String str = map.get(elementid);
              if ((null != str) && (!str.equals(""))){
                  result = true;
                  break;
              }
        }
        return  result;
    }

    public  String getvlan(String elementid ,Map<String,Map<String,String>>  hubelementgroup){
        String vlan ="";
        for(String  vlanStr : hubelementgroup.keySet()) {
          Map<String, String> map = hubelementgroup.get(vlanStr); {
                String str = map.get(elementid);
                if ((null != str) && (!str.equals(""))) {
                    vlan = vlanStr;
                    break;
                }
            }
        }
        return  vlan;
    }



    public Map<String,String>  genelementLink(String elementId1,String  elementId2){
        Map<String,String>  result=  new HashMap<>();
        result.put("selementid1",elementId1);
        result.put("selementid2",elementId2);
        return  result;
    }



    /**
     * 创建元素
     * @param elementlist
     * @param mapid
     * @return
     */
    public  Map<String,String>  createElement(List<Map<String,String>> elementlist ,String mapid){
        Zabbix zabbix = Zabbix.getZabbix();
        Map<String,Object> params = new HashMap<String,Object>();
        params.put("output","extend");
        params.put("sysmapid",mapid);
        params.put("selements",elementlist);
        Map<String, Object> result = zabbix.call("map.update", params);
        Map<String, Object> mapList = (Map<String, Object>) result.get("result");
        Map<String,String>  value = new HashMap<>();
        if(mapList.size() >0){
            if(null !=mapList.get("selements")){
                List<Map<String, Object>>  resultlist= (List<Map<String, Object>>)mapList.get("selements");
                if(resultlist.size() >0){
                    for(Map<String, Object>  obj : resultlist){
                        if(null != obj){
                            String  elementid  = obj.get("elementid").toString();
                            String  selementid = obj.get("selementid").toString();
                            if((!elementid.equals(""))  && (!selementid.equals(""))){
                                value.put(elementid,selementid);
                            }
                        }
                    }
                }
            }
        }
        return  value;
    }

    public  boolean hasElementByMap(String hostid, String mapid){
        Zabbix zabbix = Zabbix.getZabbix();
        Map<String,Object> params = new HashMap<String,Object>();
        params.put("output", "extend");
        Map<String,String> filter = new HashMap<>();
        filter.put("elementid",hostid);
      //  params.put("filter",filter);
        params.put("sysmapids",mapid);
        params.put("selectSelements","extend");
        params.put("selectLinks","extend");
        params.put("selectUsers","extend");
        params.put("selectUserGroups","extend");
        Map<String, Object> result = zabbix.call("map.get", params);
        List<Map<String, Object>> mapList = (List<Map<String, Object>>) result.get("result");
        boolean values =false;
        if(mapList.size() >0) {
           for(Map map : mapList){
                if((null != map) && (null !=map.get("selements"))){
                    List<Map<String, Object>> elementList =(List<Map<String, Object>>)map.get("selements");
                    for(Map elementMap : elementList){
                        if((null != elementMap) && (null != elementMap.get("elementid"))){
                            if(hostid.equals(elementMap.get("elementid").toString())){
                                values =true;
                            }
                        }
                    }
               }
           }
        }
        return  values;
    }
    /**
     * 创建主机
     * @param ipStr
     * @return
     */
    public String   createHost(String  ipStr,String groupid){
        String value ="";
        Zabbix zabbix = Zabbix.getZabbix();
        Map<String,Object> params = new HashMap<String,Object>();
        params.put("host",ipStr);
     //   params.put("name",ipStr);
        Map<String,String>  mapgroups = new HashMap<>();
        mapgroups.put("groupid",groupid);
        params.put("groups",mapgroups);
        Map<String,String> mapinterface = new HashMap<>();
        mapinterface.put("type","1");
        mapinterface.put("main","1");
        mapinterface.put("useip","1");
        mapinterface.put("ip",ipStr);
        mapinterface.put("dns","");
        mapinterface.put("port","10050");
        params.put("interfaces",mapinterface);
        Map<String, Object> result = zabbix.call("host.create", params);
        Map<String, Object> mapList = (Map<String, Object>) result.get("result");
        if ((null !=mapList)&& (mapList.size() >0)){
            if(null !=mapList.get("hostids")){
                value = mapList.get("hostids").toString();
            }
        }
        return  value;
    }



    /**
     * 查询拓扑图数据
     * @param mapid
     * @return
     */
    public List<Map<String,Object>>  querysysMap(String mapid){
        Zabbix zabbix = Zabbix.getZabbix();
        Map<String,Object> params = new HashMap<String,Object>();
        params.put("output", "extend");
        params.put("selectSelements", "extend");
        params.put("selectLinks", "extend");
        params.put("selectUser","enxtnd");
        params.put("selectUserGroups","extend");
        params.put("sysmapids", mapid);
        Map<String, Object> result = zabbix.call("map.get", params);
        List<Map<String, Object>> mapList = (List<Map<String, Object>>) result.get("result");
        return mapList;
    }

    /**
     *  根据组名称，来查询组的id
     * @param name
     * @return
     */
    public String  queryGroupbyName(String name){
        Zabbix zabbix = Zabbix.getZabbix();
        Map<String,Object> params = new HashMap<String,Object>();
        params.put("output","extend");
        Map<String,String> filter = new HashMap<>();
        filter.put("name",name);
        params.put("filter",filter);
        Map<String, Object> result = zabbix.call("hostgroup.get", params);
        List<Map<String, Object>> mapList = (List<Map<String, Object>>) result.get("result");
        String value ="";
        if( mapList.size() >0){
            for(Map map : mapList){
                if(null !=map.get("groupid")){
                    value = map.get("groupid").toString();
                }
            }
        }
       return  value;
    }

    /**
     * 通过id获取hostid
     * @param hostid 主机id
     * @return
     */
    public Map<String,Object> getHostById(String hostid) {
        Zabbix zabbix = Zabbix.getZabbix();
        Map<String,Object> params = new HashMap<String,Object>();
        params.put("output", "extend");
        params.put("hostids", hostid);
        params.put("selectInventory","extend"); // 主机资产记录
//        params.put("selectItems","extend"); // 监控项
        Map<String, Object> result = zabbix.call("host.get", params);
        List<Map<String, Object>> mapList = (List<Map<String, Object>>) result.get("result");
        if (mapList.size() == 1) {
            return mapList.get(0);
        }
        return null;
    }


    /********************************
     * 以下是获取端口流量
     ********************************/

    /**
     * 获取所有主机：host(ipaddr) ---> hostid(elementid)
     * @return
     */
    public Map<String,String> getAllHosts() {
        Zabbix zabbix = Zabbix.getZabbix();
        Map<String,Object> params = new HashMap<String,Object>();
        params.put("output", "extend");
        Map<String, Object> result = zabbix.call("host.get", params);
        List<Map<String, Object>> mapList = (List<Map<String, Object>>) result.get("result");
        Map resultMap = new HashMap<>();
        for (Map<String, Object> item : mapList) {
            resultMap.put(item.get("host"), item.get("hostid"));
        }
        return resultMap;
    }
    /**
     * 获取交换机入口流量数据, portname ---> 流量
     * @return
     */
    public Map<String,String> getExchangerIncoming() {
        Zabbix zabbix = Zabbix.getZabbix();
        Map<String,Object> params = new HashMap<String,Object>();
        params.put("application", "indata");
        Map<String, Object> result = zabbix.call("item.get", params);
        List<Map<String, Object>> mapList = (List<Map<String, Object>>) result.get("result");
        return this.organizeItemInOrOut(mapList);
    }

    /**
     * 获取交换机入口流量数据, portname ---> 流量
     * @return
     */
    public Map<String,String> getExchangerOutgoing() {
        Zabbix zabbix = Zabbix.getZabbix();
        Map<String,Object> params = new HashMap<String,Object>();
        params.put("application", "outdata");
        Map<String, Object> result = zabbix.call("item.get", params);
        List<Map<String, Object>> mapList = (List<Map<String, Object>>) result.get("result");
        return this.organizeItemInOrOut(mapList);
    }

    /**
     * ipmac应用集数据，即：ip-->10进制mac
     * @return
     */
    public Map<String,String> getIpMac() {
        Zabbix zabbix = Zabbix.getZabbix();
        Map<String,Object> params = new HashMap<String,Object>();
        params.put("application", "ipmac");
        Map<String, Object> result = zabbix.call("item.get", params);
        List<Map<String, Object>> mapList = (List<Map<String, Object>>) result.get("result");
        return this.organizeItemIpMac(mapList);
    }

    /**
     * macport应用集数据，即：10进制mac-->port端口号
     * @return
     */
    public Map<String,String> getMacPort() {
        Zabbix zabbix = Zabbix.getZabbix();
        Map<String,Object> params = new HashMap<String,Object>();
        params.put("application", "macport");
        Map<String, Object> result = zabbix.call("item.get", params);
        List<Map<String, Object>> mapList = (List<Map<String, Object>>) result.get("result");
        return this.organizeItem3(mapList);
    }

    /**
     * portname应用集数据，即：port-->name: port名称
     * @return
     */
    public Map<String,String> getPortName() {
        Zabbix zabbix = Zabbix.getZabbix();
        Map<String,Object> params = new HashMap<String,Object>();
        params.put("application", "portname");
        Map<String, Object> result = zabbix.call("item.get", params);
        List<Map<String, Object>> mapList = (List<Map<String, Object>>) result.get("result");
        return this.organizeItem3(mapList);
    }

    /**
     * 通过hostid到端口流量
     * @return
     */
    public Map<String,Object> getHostIdToIncomingAndOutgoingData() {
        // 1. 所有主机host(ipaddr) --> item(hostid)
        Map<String,String> allHostMap = this.getAllHosts();

        // 2. 10进制mac ---> port
        Map<String,String> macportMap = this.getMacPort();

        // 3. port ---> name
        Map<String,String> portnameMap = this.getPortName();

        // 4. 入口流量
        Map<String,String> incomingDataMap = this.getExchangerIncoming();

        // 5. 出口流量
        Map<String,String> outgoingDataMap = this.getExchangerOutgoing();

        // 4. ip --> 10进制mac
        Map<String,String> ipmacMap = this.getIpMac();
        Map<String,Object> resultMap = new HashMap<>();
        for (String ip : ipmacMap.keySet()) {
            String macStr = ipmacMap.get(ip);
            if (macStr != null) {
                String portStr = macportMap.get(macStr);
                if (portStr != null) {
                    String portnameStr = portnameMap.get(portStr);
                    String hostid = allHostMap.get(ip);
                    if (hostid != null && !hostid.isEmpty() && portnameStr != null && !portnameStr.isEmpty()) {
                        portnameStr = portnameStr.replaceAll("[a-zA-Z ]","" );
                        String incomingData = incomingDataMap.get(portnameStr);
                        String outgoingData = outgoingDataMap.get(portnameStr);
                        Map<String,String> inoroutMap = new HashMap<>();
                        inoroutMap.put("incoming", incomingData);
                        inoroutMap.put("outgoing", outgoingData);
                        resultMap.put(hostid, inoroutMap);
                    }
                }
            }
        }
        return resultMap;
    }

    /**
     * 过滤非接口数据3
     * @param itemList
     * @return
     */
    private Map<String, String> organizeItem3(List<Map<String, Object>> itemList) {
        Map<String, String> newResult = new HashMap<>();
        for (Map<String, Object> item : itemList) {
            String name = (String) item.get("name");
            String key = (String) item.get("key_");
            String[] keyArr = key.split(",");
            StringBuffer emailPattern = new StringBuffer(".*\\[");
            for (int i = 0; i < keyArr.length-1; ++i) {
                emailPattern.append("(.*),");
            }
            emailPattern.append("(.*)\\]");
            Pattern pattern = Pattern.compile(emailPattern.toString());
            Matcher matcher = pattern.matcher(key);
            Boolean isMatch = matcher.find();
            if (isMatch) {
                name = matcher.replaceAll(name);
                item.put("name", name);
            }
            item.put("id", item.get("itemid"));
            // item.put("leaf", true);
            // item.put("type", 5); // 监控项类型属性
            newResult.put((String) item.get("name"), (String) item.get("lastvalue"));
        }
        return newResult;
    }

    /**
     * 过滤非接口数据: 为入口流量和出口流量
     * @param itemList
     * @return
     */
    private Map<String, String> organizeItemInOrOut(List<Map<String, Object>> itemList) {
        Map<String, String> newResult = new HashMap<>();
        for (Map<String, Object> item : itemList) {
            String name = (String) item.get("name");
            String key = (String) item.get("key_");
            String[] keyArr = key.split(",");
            StringBuffer emailPattern = new StringBuffer(".*\\[");
            for (int i = 0; i < keyArr.length-1; ++i) {
                emailPattern.append("(.*),");
            }
            emailPattern.append("(.*)\\]");
            Pattern pattern = Pattern.compile(emailPattern.toString());
            Matcher matcher = pattern.matcher(key);
            Boolean isMatch = matcher.find();
            if (isMatch) {
                name = matcher.replaceAll(name);
                item.put("name", name);
            }
            item.put("id", item.get("itemid"));
            // item.put("leaf", true);
            // item.put("type", 5); // 监控项类型属性
            String portName = (String) item.get("name");
            // portName = portName.substring(portName.lastIndexOf(" ")+1);
            portName = portName.replaceAll("[a-zA-Z ]","" );
            newResult.put(portName, (String) item.get("lastvalue"));
        }
        return newResult;
    }

    /**
     * 过滤非接口数据4, 将16进制mac地址转换为10进制mac,如：a0:36:9f:a3:47:1c
     * @param itemList
     * @return
     */
    private Map<String, String> organizeItemIpMac(List<Map<String, Object>> itemList) {
        Map<String, String> newResult = new HashMap<>();
        for (Map<String, Object> item : itemList) {
            String name = (String) item.get("name");
            String key = (String) item.get("key_");
            String[] keyArr = key.split(",");
            StringBuffer emailPattern = new StringBuffer(".*\\[");
            for (int i = 0; i < keyArr.length-1; ++i) {
                emailPattern.append("(.*),");
            }
            emailPattern.append("(.*)\\]");
            Pattern pattern = Pattern.compile(emailPattern.toString());
            Matcher matcher = pattern.matcher(key);
            Boolean isMatch = matcher.find();
            if (isMatch) {
                name = matcher.replaceAll(name);
                item.put("name", name);
            }
            item.put("id", item.get("itemid"));
            // item.put("leaf", true);
            // item.put("type", 5); // 监控项类型属性
            String macStr = (String) item.get("lastvalue");
            // 16进制mac地址转10进制
            String[] hexValues = macStr.split(" ");
            String tenMacStr = "";
            for (String hexValue: hexValues) {
                Integer tenInt = Integer.parseInt(hexValue, 16);
                tenMacStr += tenInt.toString() + ".";
            }
            tenMacStr = tenMacStr.substring(0, tenMacStr.length()-1);

            // name
            String ipName = (String) item.get("name");
            ipName = ipName.substring(ipName.indexOf(".")+1);

            newResult.put(ipName, tenMacStr);
        }
        return newResult;
    }

}
